import {BrowserView, BrowserWindow} from "electron";
import {IpcChannel} from "../ipc";
import {BrowserDemoUrl, IsUseSysTitle} from "../config/const";
import {otherWindowConfig} from "../config/windowsConfig";
import {browserDemoURL} from "../config/staticPath";
import {openDevTools} from "./devToolHook";

let dragTabOffsetX: number;
let lastDragView: BrowserView;
let emptyWin: BrowserWindow;
let viewFromWin: BrowserWindow;
let useNewWindow: BrowserWindow;
const winList: BrowserWindow[] = [];
const viewList: BrowserView[] = [];
let startScreenY: number;

export function openBrowserDemoWindow() {
	let win = new BrowserWindow({
		titleBarStyle: IsUseSysTitle ? "default" : "hidden",
		...Object.assign(otherWindowConfig, {}),
	});
	// // 开发模式下自动开启devtools
	if (process.env.NODE_ENV === "development") {
		openDevTools(win);
	}
	win.loadURL(browserDemoURL);
	win.on("ready-to-show", () => {
		win.show();
	});
	win.on("closed", () => {
		const findIndex = winList.findIndex((v) => win === v);
		if (findIndex !== -1) {
			winList.splice(findIndex, 1);
		}
	});
	winList.push(win);
	return win;
}

export function createDefaultBrowserView(
	win: BrowserWindow,
	defaultUrl = BrowserDemoUrl
) {
	const [winWidth, winHeight] = win.getSize();
	const bv = new BrowserView();
	win.addBrowserView(bv);
	// title-bar 30px  tabbar 40px  searchbar 40px
	bv.setBounds({x: 0, y: 110, width: winWidth, height: winHeight - 110});
	bv.setAutoResize({
		width: true,
		height: true,
	});
	bv.webContents.on("did-finish-load", () => {
		console.log(bv.webContents.getURL());
	});
	bv.webContents.loadURL(defaultUrl);
	bv.webContents.on("page-title-updated", (event, title) => {
		const parentBw = BrowserWindow.fromBrowserView(bv);
		if (parentBw) {
			freshTabData(parentBw, bv, 1);
		}
	});
	bv.webContents.on("destroyed", () => {
		const findIndex = viewList.findIndex((v) => v === bv);
		if (findIndex !== -1) {
			viewList.splice(findIndex, 1);
		}
	});
	bv.webContents.setWindowOpenHandler((details) => {
		const parentBw = BrowserWindow.fromBrowserView(bv);
		createDefaultBrowserView(parentBw, details.url);
		return {action: "deny"};
	});
	freshTabData(win, bv, 1);
	viewList.push(bv);
	return bv;
}

export function addBrowserView(win: BrowserWindow, view: BrowserView) {
	if (BrowserWindow.fromBrowserView(view) !== win) {
		win.addBrowserView(view);
		win.show();
		win.setAlwaysOnTop(true);
	}
	freshTabData(win, view, 1);
}

export function removeBrowserView(win: BrowserWindow, view: BrowserView) {
	if (BrowserWindow.fromBrowserView(view) === win) {
		win.removeBrowserView(view);
	}
	freshTabData(win, view, -1);
}

export function freshTabData(
	win: BrowserWindow,
	bv: BrowserView,
	status: -1 | 1
) {
	win.webContents.send(IpcChannel.BrowserViewTabDataUpdate, {
		bvWebContentsId: bv.webContents.id,
		title: bv.webContents.getTitle(),
		url: bv.webContents.getURL(),
		status: status,
	});
}

export const useHookBrowser = {
	openBrowserDemoWindow,
	createDefaultBrowserView,
	addBrowserView,
	removeBrowserView,
	freshTabData,
};
